package com.example.e_book.Fragment;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.e_book.DAO.BookDAO;
import com.example.e_book.DBUtil.DBHelper;
import com.example.e_book.R;
import com.example.e_book.entity.Book;
import com.example.e_book.Adapter.BookAdapter; // 导入外部的BookAdapter
import com.example.e_book.entity.User;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<Book> bookList;
    private BookAdapter bookAdapter;
    private BookDAO bookDAO;
    private SearchView searchView;
    private User user;

    @SuppressLint("Range")
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 获取从 MainActivity 传递过来的用户信息
        if (getArguments() != null) {
            user = getArguments().getParcelable("USER");
        }

        recyclerView = view.findViewById(R.id.recyclerViewBooks);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        bookDAO = new BookDAO(getContext());
        bookList = new ArrayList<>();
        Cursor cursor = bookDAO.getAllBooks();
        if (cursor.moveToFirst()) {
            do {
                Book book = new Book();
                book.setBookId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_BOOK_ID)));
                book.setTitle(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BOOK_TITLE)));
                book.setAuthor(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BOOK_AUTHOR)));
                book.setCategory(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_CATEGORY)));
                book.setContentPath(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BOOK_MAIN_CONTENT)));
                book.setCoverImagePath(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_BOOK_COVER_IMAGE)));
                bookList.add(book);
            } while (cursor.moveToNext());
            cursor.close();
        }

        bookAdapter = new BookAdapter(bookList,getContext(),user); // 使用外部的BookAdapter
        recyclerView.setAdapter(bookAdapter);

        searchView = view.findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchBooks(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                searchBooks(newText);
                return true;
            }
        });

        return view;
    }

    private void searchBooks(String query) {
        List<Book> filteredList = new ArrayList<>();
        for (Book book : bookList) {
            if (book.getTitle().toLowerCase().contains(query.toLowerCase()) ||
                    String.valueOf(book.getBookId()).contains(query) ||
                    book.getAuthor().toLowerCase().contains(query.toLowerCase())) {
                filteredList.add(book);
            }
        }
        bookAdapter.updateList(filteredList);
        bookAdapter.notifyDataSetChanged();
    }
}